package com.lancoo.znbkxx.teacher.ui.fragment

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.adapter.EvenItemDecoration
import com.lancoo.znbkxx.teacher.adapter.ReviewedAdapter
import com.lancoo.znbkxx.teacher.adapter.StuProgressAdapter
import com.lancoo.znbkxx.teacher.base.BaseVMFragment
import com.lancoo.znbkxx.teacher.databinding.FmtStuProgressBinding
import com.lancoo.znbkxx.teacher.model.data.NotReviewed
import com.lancoo.znbkxx.teacher.model.data.StuProgressInfo
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import com.lancoo.znbkxx.teacher.viewmodel.CommentViewModel
import java.io.Serializable

/**
 * @author : Admin
 * @date   : 2019/11/28 20:33
 * @desc   :
 */
class ReviewedFragment : BaseVMFragment<CommentViewModel, FmtStuProgressBinding>() {
    private val submitList = mutableListOf<StuProgressInfo>()
    private val reviewList = mutableListOf<NotReviewed>()
    private lateinit var adapter: ReviewedAdapter
    private lateinit var submittedAdapter: StuProgressAdapter
    override fun setLayoutID(): Int {
        return R.layout.fmt_stu_progress
    }

    override fun providerContext(): Any? {
        return this
    }

    override fun providerVMClass(): Class<CommentViewModel>? {
        return CommentViewModel::class.java
    }

    companion object {
        private const val REVIEWED = "reviewed"
        private const val SUBMITTED = "submitted"
        fun newInstance(
                reviewedList: MutableList<StuProgressInfo>?,
                submittedList: MutableList<StuProgressInfo>?
        ): ReviewedFragment {
            val fragment = ReviewedFragment()
            val bundle = Bundle()
            if (!submittedList.isNullOrEmpty()) {
                bundle.putSerializable(SUBMITTED, submittedList as Serializable)
            }
            if (!reviewedList.isNullOrEmpty()) {
                bundle.putSerializable(REVIEWED, reviewedList as Serializable)
            }
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun initView() {
        binding.rv.layoutManager =
            GridLayoutManager(context, 3)
        binding.rv.addItemDecoration(EvenItemDecoration(DensityUtils.dp2px(activity, 10f), 3))
        adapter = ReviewedAdapter(R.layout.item_stu_progress, reviewList)
        submittedAdapter =
            StuProgressAdapter(R.layout.item_stu_progress, submitList)
        if (null != arguments?.getSerializable("reviewed")) {
            reviewList.addAll(arguments?.getSerializable("reviewed") as MutableList<NotReviewed>)
            binding.rv.adapter = adapter
            adapter.setNewData(reviewList)
        } else if (null != arguments?.getSerializable("submitted")) {
            submitList.addAll(arguments?.getSerializable("submitted") as MutableList<StuProgressInfo>)
            binding.rv.adapter = submittedAdapter
            submittedAdapter.setNewData(submitList)
        } else {
            adapter.emptyView = layoutInflater.inflate(R.layout.item_stu_progress_empty, null)
            submittedAdapter.emptyView = layoutInflater.inflate(R.layout.item_stu_progress_empty, null)
            binding.rv.adapter = adapter
        }
    }
}